<template>
  <div class="login-container">
    <!-- 左侧装饰区域 -->
    <div class="login-left">
      <div class="left-content">
        <h1 class="main-title">
          CodeJudge<br>在线编程评测系统
        </h1>
        <p class="subtitle">
          高效、公平、智能的编程作业评测平台，助力教学与学习
        </p>
        
        <div class="features">
          <div class="feature-item">
            <div class="feature-icon">
              <el-icon><Check /></el-icon>
            </div>
            <div class="feature-text">
              <h3>自动评测</h3>
              <p>实时反馈代码运行结果</p>
            </div>
          </div>
          
          <div class="feature-item">
            <div class="feature-icon">
              <el-icon><Monitor /></el-icon>
            </div>
            <div class="feature-text">
              <h3>多语言支持</h3>
              <p>支持Python、C++、Java等多种语言</p>
            </div>
          </div>
          
          <div class="feature-item">
            <div class="feature-icon">
              <el-icon><TrendCharts /></el-icon>
            </div>
            <div class="feature-text">
              <h3>学习分析</h3>
              <p>详细的学习数据与进度跟踪</p>
            </div>
          </div>
        </div>
      </div>
      
      <!-- 装饰图形 -->
      <div class="decoration-circle circle-1"></div>
      <div class="decoration-circle circle-2"></div>
      <div class="decoration-circle circle-3"></div>
    </div>

    <!-- 右侧登录表单 -->
    <div class="login-right">
      <div class="login-form-container">
        <!-- 移动端Logo -->
        <div class="mobile-logo">
          <div class="logo-icon">
            <el-icon><Monitor /></el-icon>
          </div>
          <h1>CodeJudge</h1>
        </div>

        <div class="login-card">
          <h2 class="login-title">用户登录</h2>

          <el-form
            ref="loginFormRef"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
            @submit.prevent="handleLogin"
          >
            <!-- 角色选择 -->
            <div class="role-selector">
              <label class="role-label">账号类型</label>
              <div class="role-options">
                <label 
                  v-for="role in roles" 
                  :key="role.value"
                  class="role-option"
                  :class="{ active: selectedRole === role.value }"
                >
                  <input 
                    type="radio" 
                    :value="role.value" 
                    v-model="selectedRole"
                    class="role-radio"
                  >
                  <div class="role-content">
                    <el-icon><component :is="role.icon" /></el-icon>
                    <span>{{ role.label }}</span>
                  </div>
                </label>
              </div>
            </div>

            <el-form-item prop="username">
              <el-input
                v-model="loginForm.username"
                placeholder="请输入用户名"
                size="large"
                prefix-icon="User"
                class="custom-input"
              />
            </el-form-item>
            
            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                type="password"
                placeholder="请输入密码"
                size="large"
                prefix-icon="Lock"
                show-password
                class="custom-input"
                @keyup.enter="handleLogin"
              />
            </el-form-item>
            
            <el-form-item>
              <el-button
                type="primary"
                size="large"
                :loading="loading"
                class="login-button"
                @click="handleLogin"
              >
                登录
              </el-button>
            </el-form-item>
          </el-form>

          <div class="login-footer">
            <span>还没有账号?</span>
            <el-link type="primary" @click="handleRegisterClick">
              {{ registerButtonText }}
            </el-link>
          </div>
        </div>

        <div class="system-info">
          在线编程评测系统
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { Check, Monitor, TrendCharts, User as UserIcon, Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'

const router = useRouter()
const authStore = useAuthStore()

const loginFormRef = ref<FormInstance>()
const loading = ref(false)
const selectedRole = ref('student')

const loginForm = reactive({
  username: '',
  password: ''
})

const roles = [
  { value: 'student', label: '学生', icon: 'User' },
  { value: 'teacher', label: '教师', icon: 'UserFilled' },
  { value: 'admin', label: '管理员', icon: 'Setting' }
]

const loginRules: FormRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
    loading.value = true
    
    const success = await authStore.login(loginForm.username, loginForm.password)
    if (success) {
      router.push('/dashboard')
    }
  } catch (error) {
    console.error('登录失败:', error)
  } finally {
    loading.value = false
  }
}

// 注册按钮文本
const registerButtonText = computed(() => {
  switch (selectedRole.value) {
    case 'student':
      return '联系管理员注册'
    case 'teacher':
      return '联系管理员注册'
    case 'admin':
      return '注册新账号'
    default:
      return '联系管理员注册'
  }
})

// 处理注册点击
const handleRegisterClick = () => {
  console.log('注册按钮被点击，当前角色:', selectedRole.value)
  
  if (selectedRole.value === 'admin') {
    // 管理员可以注册新账号
    console.log('跳转到注册页面')
    ElMessage.info('跳转到注册页面...')
    router.push('/register')
  } else {
    // 学生和教师需要联系管理员
    console.log('显示联系管理员提示')
    ElMessage.info('请联系系统管理员为您创建账号')
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  min-height: 100vh;
  font-family: 'Inter', system-ui, sans-serif;
}

/* 左侧装饰区域 */
.login-left {
  flex: 1;
  background: linear-gradient(135deg, #6366F1 0%, #4F46E5 100%);
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  padding: 3rem;
}

.left-content {
  position: relative;
  z-index: 10;
  color: white;
  max-width: 500px;
}

.main-title {
  font-size: clamp(2rem, 5vw, 3.5rem);
  font-weight: bold;
  line-height: 1.2;
  margin-bottom: 1.5rem;
}

.subtitle {
  font-size: 1.125rem;
  color: rgba(255, 255, 255, 0.8);
  margin-bottom: 2rem;
  max-width: 400px;
}

.features {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.feature-icon {
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.feature-text h3 {
  font-weight: 500;
  margin-bottom: 0.25rem;
}

.feature-text p {
  font-size: 0.875rem;
  color: rgba(255, 255, 255, 0.7);
  margin: 0;
}

/* 装饰图形 */
.decoration-circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 6s ease-in-out infinite;
}

.circle-1 {
  width: 8rem;
  height: 8rem;
  top: 2.5rem;
  right: 2.5rem;
}

.circle-2 {
  width: 10rem;
  height: 10rem;
  bottom: 5rem;
  left: 2.5rem;
  animation-delay: 1s;
}

.circle-3 {
  width: 6rem;
  height: 6rem;
  top: 50%;
  left: 33%;
  animation-delay: 2s;
}

@keyframes float {
  0%, 100% { transform: translateY(0px); }
  50% { transform: translateY(-20px); }
}

/* 右侧登录表单 */
.login-right {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  background: #f9fafb;
}

.login-form-container {
  width: 100%;
  max-width: 28rem;
}

.mobile-logo {
  display: none;
  align-items: center;
  justify-content: center;
  margin-bottom: 2rem;
}

.logo-icon {
  width: 3rem;
  height: 3rem;
  border-radius: 0.5rem;
  background: #6366F1;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 0.75rem;
  font-size: 1.5rem;
}

.mobile-logo h1 {
  font-size: 1.5rem;
  font-weight: bold;
  color: #1f2937;
}

.login-card {
  background: white;
  border-radius: 1rem;
  padding: 2rem;
  box-shadow: 0 10px 25px -5px rgba(99, 102, 241, 0.1), 0 8px 10px -6px rgba(99, 102, 241, 0.1);
}

.login-title {
  font-size: 1.5rem;
  font-weight: bold;
  text-align: center;
  margin-bottom: 1.5rem;
  color: #1f2937;
}

/* 角色选择器 */
.role-selector {
  margin-bottom: 1.25rem;
}

.role-label {
  display: block;
  font-size: 0.875rem;
  font-weight: 500;
  color: #374151;
  margin-bottom: 0.25rem;
}

.role-options {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.75rem;
}

.role-option {
  position: relative;
  cursor: pointer;
}

.role-radio {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.role-content {
  height: 3rem;
  border: 2px solid #e5e7eb;
  border-radius: 0.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.875rem;
  font-weight: 500;
  color: #6b7280;
  transition: all 0.2s;
  gap: 0.5rem;
}

.role-option.active .role-content {
  border-color: #6366F1;
  color: #6366F1;
}

.role-option:hover .role-content {
  border-color: #6366F1;
  color: #6366F1;
}

/* 表单样式 */
.login-form {
  margin-top: 1.25rem;
}

.custom-input :deep(.el-input__wrapper) {
  border-radius: 0.5rem;
  border: 1px solid #d1d5db;
  transition: all 0.2s;
}

.custom-input :deep(.el-input__wrapper:hover) {
  border-color: #6366F1;
}

.custom-input :deep(.el-input__wrapper.is-focus) {
  border-color: #6366F1;
  box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);
}

.login-button {
  width: 100%;
  height: 2.75rem;
  font-size: 1rem;
  font-weight: 500;
  border-radius: 0.5rem;
  background: #6366F1;
  border: none;
  transition: all 0.2s;
}

.login-button:hover {
  background: #4F46E5;
  transform: translateY(-2px);
  box-shadow: 0 10px 15px -3px rgba(99, 102, 241, 0.2);
}

.login-button:active {
  transform: translateY(0);
}

.login-footer {
  text-align: center;
  margin-top: 1.5rem;
  font-size: 0.875rem;
  color: #6b7280;
}

.login-footer span {
  margin-right: 0.25rem;
}

.system-info {
  text-align: center;
  margin-top: 2rem;
  color: #9ca3af;
  font-size: 0.875rem;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
  }
  
  .login-left {
    display: none;
  }
  
  .mobile-logo {
    display: flex;
  }
  
  .login-right {
    padding: 1rem;
  }
  
  .login-card {
    padding: 1.5rem;
  }
}
</style>
